package com.sanweibook.jhook.server.facade.sys.impl;

import com.sanweibook.jhook.biz.sys.SysUserService;
import com.sanweibook.jhook.common.result.Result;
import com.sanweibook.jhook.common.util.JhookObjectUtils;
import com.sanweibook.jhook.dal.model.sys.SysUser;
import com.sanweibook.jhook.dubbo.dto.sys.SysUserDTO;
import com.sanweibook.jhook.dubbo.facade.sys.SysUserFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Optional;

/**
 * Created by twg on 2017/4/6.
 */
@Service("rpcSysUserFacade")
public class SysUserFacadeImpl implements SysUserFacade {
    @Autowired
    private SysUserService sysUserService;

    @Override
    public Result<SysUserDTO> getSysUser(String userName) {
        Assert.notNull( userName, "userName must not empty." );
        SysUser sysUser = new SysUser();
        sysUser.setUserName( userName );
        Optional<SysUser> user = Optional.ofNullable( sysUserService.get( sysUser ) );
        return Result.returnSuccessResult( JhookObjectUtils.objectToObject( user.get(), SysUserDTO.class ) );
    }

    public Result<SysUserDTO> getSysUserByNameAndPassword(String userName, String userPassword) {
        Assert.notNull( userName, "userName must not empty." );
        Assert.notNull( userPassword, "userPassword must not empty." );
        SysUser sysUser = new SysUser();
        sysUser.setUserName( userName );
        sysUser.setUserPassword( userPassword );
        Optional<SysUser> user = Optional.ofNullable( sysUserService.get( sysUser ) );
        return Result.returnSuccessResult( JhookObjectUtils.objectToObject( user.get(), SysUserDTO.class ) );
    }

    @Override
    public Result<List<SysUserDTO>> querySysUserByIds(List<Long> ids) {
        List<SysUser> sysUsers = sysUserService.queryByIds( ids );
        return Result.returnSuccessResult( JhookObjectUtils.listToList( sysUsers, SysUserDTO.class ) );
    }
}
